package com.wang.util;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.mozilla.universalchardet.UniversalDetector;

import java.io.UnsupportedEncodingException;

/**
 * @author wcy
 * @date 2022-08-29 15:10
 */
public class GetByteEncodeUtil {


    public static void main(String[] args) throws UnsupportedEncodingException {
        byte[] aa = {0x7B, 0x22, 0x64, 0x65, 0x76, 0x47, 0x49, 0x64, 0x22, 0x3A, 0x22, 0x4C
                , 0x48, 0x2D, 0x47, 0x38, 0x38, 0x35, 0x30, 0x30, 0x30, 0x31
                , 0x5F, 0x4C, 0x4D, 0x32, 0x31, 0x41, 0x32, 0x32
                , 0x30, 0x39, 0x30, 0x30, 0x31, 0x22, 0x2C, 0x22, 0x63, 0x6F
                , 0x6D, 0x6D, 0x61, 0x6E, 0x64, 0x22, 0x3A, 0x22, 0x73, 0x65, 0x6E, 0x64, 0x44
                , 0x65, 0x76, 0x4C, 0x6F, 0x67, 0x22, 0x2C, 0x22, 0x63, 0x6F, 0x6D, 0x6D, 0x61, 0x6E, 0x64
                , 0x54, 0x79, 0x70, 0x65, 0x22, 0x3A, 0x22, 0x73, 0x65, 0x6E, 0x64, 0x22, 0x2C, 0x22, 0x64, 0x61, 0x74
                , 0x61, 0x22, 0x3A, 0x7B, 0x22, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x54, 0x69, 0x6D, 0x65, 0x22, 0x3A
                , 0x22, 0x32, 0x30, 0x32, 0x32, 0x2D, 0x30, 0x36, 0x2D, 0x32, 0x31, 0x20, 0x32, 0x31, 0x3A, 0x30, 0x31
                , 0x3A, 0x31, 0x34, 0x22, 0x2C, 0x22, 0x64, 0x65, 0x76, 0x45, 0x76, 0x65, 0x6E, 0x74, 0x54, 0x79, 0x70
                , 0x65, 0x43, 0x6F, 0x64, 0x65, 0x22, 0x3A, 0x22, 0x30, 0x36, 0x30, 0x30, 0x22, 0x2C, 0x22, 0x64, 0x65, 0x76
                , 0x4C, 0x6F, 0x67, 0x43, 0x6F, 0x6E, 0x74, 0x65, 0x6E, 0x74, 0x22, 0x3A, 0x22, (byte) 0xCD, (byte) 0xA3, (byte) 0xD6, (byte) 0xB9, 0x22, 0x2C
                , 0x22, 0x6F, 0x70, 0x65, 0x72, 0x61, 0x74, 0x65, 0x55, 0x73, 0x65, 0x72, 0x22, 0x3A, 0x22, 0x61
                , 0x64, 0x6D, 0x69, 0x6E, 0x22, 0x7D, 0x2C, 0x22, 0x6D, 0x73, 0x67, 0x22, 0x3A, 0x22, 0x22, 0x2C, 0x22, 0x72, 0x65, 0x73, 0x75
                , 0x6C, 0x74, 0x22, 0x3A, 0x22, 0x22, 0x7D};


        System.out.println(getEncoding(aa));
        System.out.println(isUTF8(aa));


        System.out.println(new String(aa, "GBK"));
        String pp = "{\"devGId\":\"LH-G8850001_LM21A2209001\",\"command\":\"sendDevLog\",\"commandType\":\"send\",\"data\":{\"createTime\":\"2022-06-21 21:01:14\",\"devEventTypeCode\":\"0600\",\"devLogContent\":\"停止\",\"operateUser\":\"admin\"},\"msg\":\"\",\"result\":\"\"}";


        String rr = "启动";




       /* System.out.println(pp);
        String gbkStr = new String(rr.getBytes(),"GBK");
        System.out.println(ModBusUtil.bytes2HexString(gbkStr.getBytes()));
        String utf8Str = new String(rr.getBytes(),"UTF-8");*/
        String gbkStr = new String(rr.getBytes(), "GBK");
        System.out.println(gbkStr);
        System.out.println(encodeHexString(rr.getBytes()));


        byte[] bb = {0x3A, 0x22};
        System.out.println(new String(bb));


    }

    public static String encodeHexString(byte[] data) {
        StringBuilder sb = new StringBuilder();
        for (byte b : data) {
            sb.append(String.format("%02x", b));
        }
        return sb.toString();
    }


    public static String getEncoding(byte[] bytes) {

        String defaultEncoding = "UTF-8";
        String defaultEncoding2 = "GBK";

        UniversalDetector detector = new UniversalDetector(null);

        detector.handleData(bytes, 0, bytes.length);

        detector.dataEnd();

        String encoding = detector.getDetectedCharset();

        detector.reset();


        if (encoding != null && encoding.equals("UTF-8")) {


            encoding = defaultEncoding;

        } else {
            encoding = defaultEncoding2;
        }

        return encoding;

    }

    public static boolean isUTF8(byte[] rawtext) {
        int score = 0;
        int i, rawtextlen = 0;
        int goodbytes = 0, asciibytes = 0;
        // Maybe also use UTF8 Byte Order Mark: EF BB BF
        // Check to see if characters fit into acceptable ranges
        rawtextlen = rawtext.length;
        for (i = 0; i < rawtextlen; i++) {
            if ((rawtext[i] & (byte) 0x7F) == rawtext[i]) {
                // 最高位是0的ASCII字符
                asciibytes++;
                // Ignore ASCII, can throw off count
            } else if (-64 <= rawtext[i] && rawtext[i] <= -33
                    //-0x40~-0x21
                    && // Two bytes
                    i + 1 < rawtextlen && -128 <= rawtext[i + 1]
                    && rawtext[i + 1] <= -65) {
                goodbytes += 2;
                i++;
            } else if (-32 <= rawtext[i]
                    && rawtext[i] <= -17
                    && // Three bytes
                    i + 2 < rawtextlen && -128 <= rawtext[i + 1]
                    && rawtext[i + 1] <= -65 && -128 <= rawtext[i + 2]
                    && rawtext[i + 2] <= -65) {
                goodbytes += 3;
                i += 2;
            }
        }
        if (asciibytes == rawtextlen) {
            return false;
        }
        score = 100 * goodbytes / (rawtextlen - asciibytes);
        // If not above 98, reduce to zero to prevent coincidental matches
        // Allows for some (few) bad formed sequences
        if (score > 98) {
            return true;
        } else if (score > 95 && goodbytes > 30) {
            return true;
        } else {
            return false;
        }
    }


    public static void test(byte[] b) {


        if (b[0] == -17 && b[1] == -69 && b[2] == -65)
            System.out.println("：编码为UTF-8");
        else
            System.out.println("：可能是GBK，也可能是其他编码");

    }


}
